import { getToken } from "@/utils/auth";

export async function request(url: string, method: "POST" | "GET", data: any): Promise<any> {
	let token = ''
	try {
		token = await getToken()
	} catch (err) {
		console.error(err)
		token = ''
	}

	const header = {
		'x-access-token': token
	};
	if (url.startsWith("/")) {
		url = url.slice(1)
	}
	return new Promise((resolve, reject) => {
		uni.request({
			// url: "https://wechatapi.houzhanyi.com:8787/" + url,
			url,
			method,
			data,
			header,
			success: (res) => {
				resolve(res)
				if (res.statusCode == 401) {
					uni.navigateTo({
						url: `/pages/auth/login`
					});
				}

			}, fail: (err) => {
				console.log(err)
				reject(err)
			}
		});
	})

}